package cn.ultrarisk.scadmin.manager.web.controller.solr;

import cn.ultrarisk.mybatis.extension.domain.Page;
import cn.ultrarisk.mybatis.extension.query.Query;
import cn.ultrarisk.scadmin.manager.biz.monitor.dao.SolrReplicaMonitorDAO;
import cn.ultrarisk.scadmin.manager.biz.solr.dao.SolrCollectionDAO;
import cn.ultrarisk.scadmin.manager.biz.solr.dao.SolrReplicaDAO;
import cn.ultrarisk.scadmin.manager.biz.solr.entity.SolrCollection;
import cn.ultrarisk.scadmin.manager.biz.solr.service.SolrCollectionService;
import cn.ultrarisk.scadmin.manager.web.controller.BaseController;
import cn.ultrarisk.scadmin.manager.web.dto.solr.SolrCollectionDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * CollectionController
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/4/17
 * @since V1.0
 */
@Controller
@RequestMapping(value = "/solr/collection")
public class CollectionController extends BaseController {

    @Autowired
    private SolrCollectionDAO solrCollectionDAO;
    @Autowired
    private SolrCollectionService solrCollectionService;
    @Autowired
    private SolrReplicaMonitorDAO solrReplicaMonitorDAO;
    @Autowired
    private SolrReplicaDAO solrReplicaDAO;

    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<Map<String, Object>> list(String name, int pageNo, int pageSize) {
        Map<String, Object> map = new HashMap<String, Object>();

        Page<SolrCollection> paged = solrCollectionDAO.findPage(name, pageNo, pageSize);
        map.put("paged", paged);

        List<Integer> collectionIds = new ArrayList<Integer>();
        for (SolrCollection collection : paged.getList()) {
            collectionIds.add(collection.getId());
        }
        if (collectionIds.size() > 0) {
            Map<Integer, Long> docsMap = solrReplicaMonitorDAO.getNewestCollectionDocs(collectionIds);
            map.put("docsMap", docsMap);
        }

        return getOkResponseEntity(map);
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<SolrCollection> get(@PathVariable("id") Integer id) {
        SolrCollection solrCollection = solrCollectionDAO.get(id);
        return getOkResponseEntity(solrCollection);
    }

    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<String> create(@RequestBody SolrCollectionDTO dto) {
        solrCollectionService.create(dto.getName(), dto.getConfig(), dto.getAllowCommit(), dto.getAllowRollback(),
                dto.getMaxStart(), dto.getMaxLimit(), dto.getRemark(), dto.getShardReplicas());
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    public ResponseEntity<String> update(@PathVariable("id") Integer id, @RequestBody SolrCollectionDTO dto) {
        solrCollectionService.update(id, dto.getAllowCommit(), dto.getAllowRollback(),
                dto.getMaxStart(), dto.getMaxLimit(), dto.getRemark(), dto.getShardReplicas());
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<String> delete(@PathVariable("id") Integer id) throws Exception {
        solrCollectionService.delete(id);
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/optimize", method = RequestMethod.GET)
    public ResponseEntity<String> optimize(@PathVariable("id") Integer id, Integer maxSegments) throws Exception {
        solrCollectionService.optimize(id, maxSegments);
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/reload", method = RequestMethod.GET)
    public ResponseEntity<String> reload(@PathVariable("id") Integer id) throws Exception {
        solrCollectionService.reload(id);
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/exist", method = RequestMethod.GET)
    public ResponseEntity<Boolean> exist(String name, Integer idNo) throws IOException {
        Query query = Query.query(SolrCollection.NAME, name).neq(SolrCollection.ID, idNo);
        Boolean exist = solrCollectionDAO.exist(query);
        return getOkResponseEntity(exist);
    }

    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseEntity<List<SolrCollection>> findCollections(Integer nodeId, Boolean available) {
        return getOkResponseEntity(solrCollectionDAO.findList(nodeId, available));
    }
}
